package cn.frt.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.PathParam;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.frt.pojo.Shops;
import cn.frt.pojo.Site;
import cn.frt.service.ShopsService;

@Controller
@RequestMapping(value="/shops")
public class ShopsController {
	@Resource
	private ShopsService shopsService;
	
	//运营商注册进入页面
		@RequestMapping(value="/entranceadd",method=RequestMethod.GET)
		public String entranceAdd(){
			return "/shops/shops_add";
		}
	//注册运营商
	@RequestMapping(value="/shopsadd",method=RequestMethod.POST)
	public String shopAdd(Shops shops){
		int shop=shopsService.insertShops(shops);
		if(shop>0){
			return "/shops/shops_login";
		}else{
			return "forward:/shops/entranceadd";
		}
	}
	
	//运营商登录进入页面
	@RequestMapping(value="/entrancelogin",method=RequestMethod.GET)
	public String entranceLogin(){
		return "/shops/shops_login";
	}
	//登录
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String shopsLogin(@RequestParam String shopsNumber,@RequestParam String shopsPwd,HttpServletRequest request){
		Shops shops=shopsService.shopsLogin(shopsNumber, shopsPwd);
		if(shops!=null){
			return "/shops/shops_admin";
			
		}
		request.setAttribute("msg", "错误");
		return "forward:/shops/entrancelogin";
	}
	
	//进入运营表页面
	@RequestMapping(value="/entrance",method=RequestMethod.GET)
	public String entrance(){
		return "/shops/shops_surface";
	}
	//查询运营数据
	@RequestMapping(value="/select",method=RequestMethod.POST)
	@ResponseBody
	public Object selectsShop(){
	List<Shops> list=shopsService.selectShops();
	return list;
	}
	
	//分页
	@RequestMapping(value="/page/{currPage}/{pageSize}")
	@ResponseBody
	public List<Shops> getShopsByArray(@PathVariable("currPage") int currPage,
																		@PathVariable("pageSize") int pageSize){
		List<Shops> shops = shopsService.shopsByPage(currPage, pageSize);				
		return shops;
	}
	//修改查询页面
	@RequestMapping(value="/entranceupdate")
	public String selectUpdate(Integer shopsId,Model model){
		List<Shops> list=	shopsService.selectShopsById(shopsId);
			model.addAttribute("shops", list);
			return "/shops/shops_update";
		}
	//修改运营信息
	@RequestMapping(value="/updatashops",method=RequestMethod.POST)
	public String updateShops(Shops shops){
		if(shops==null){
			return "error";
		}else{
			shopsService.updateShopsById(shops);
			return "/shops/shops_surface";
		}
	}
	//删除运营
	@RequestMapping(value="/deleteshops")
	public String deleteShops(Integer shopsId){
		if(shopsId==null){
			return "error";
		}else{
			shopsService.deleteShopsById(shopsId);
			return"/shops/shops_surface";
		}
	}
}
